<!--  -->
<template>
  
  <el-container class="home-container">

    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '230px'">
      <!-- 头部logo -->
      
        <div class="header-title" v-if="!isCollapse">
          <img width="35px" height="35px" src="../assets/logo.png" style="border-radius: 5px;">
          <span style="font-weight: bold;">导航页切换示例</span>
        </div>
        <div class="header-title-hiddle" v-else>
          <img width="40px" height="40px" src="../assets/logo.png" style="border-radius: 5px;">
        </div>
      

      <!-- 侧边栏菜单 -->
      <el-menu
        background-color="#333744"
        text-color="#fff"
        active-text-color="#409EFF"
        :collapse="isCollapse"
        :collapse-transition="false"
        :default-active="'/'+this.$store.state.activePath"
        router>

        <!-- 首页 -->
        <el-menu-item
           :index="item.path" v-for="item in menuList"
           :key="item.id"
           @click="selectMenu(item)">
          <!-- <i :class="item.icon"></i> -->
          <!-- <Icon class="xxx" icon="location"></Icon> -->
            <el-icon>
              <component :is="item.icon"></component>
            </el-icon>
          <span>{{item.label}}</span>
        </el-menu-item>

      </el-menu>
    </el-aside>

    <!-- 主体 -->
    <el-container>
      <!-- 头部 -->
      <el-header>

        <!-- 头部导航栏 -->
        <div class="header-row">
          <!-- 折叠 展开 和 名人名言 -->
          <div class="toggle-button">
            <div>
              <i :title="isCollapse ? '展开' : '收起'" class="fa fa-bars" @click="toggleCollapse"></i>
            </div>
            <div style="height:25px;width:100%;margin-left:10px">
              <div style="float:left;color:#E74405;font-size:16px;height:25px;line-height:25px;">
                <i class="fa fa-bullhorn"></i>
              </div>

              <el-carousel height="25px" direction="vertical" indicator-position="none" autoplay :interval="8000">
                <el-carousel-item v-for="item in mottoList" :key="item">
                  <h3 class="medium">{{ item }}</h3>
                </el-carousel-item>
              </el-carousel>
            </div>
          </div>

          <!-- 头像下拉菜单 -->
          <div class="header-avatar">
            <div class="user">
              管理员，您好！
            </div>
            <el-dropdown @command="handleCommand">
              <span class="el-dropdown-link">
                <img width="35" height="35" style="border-radius:50%;background:#dddddd; object-fit: cover;" :src="this.$store.state.user.picture" alt="" >
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="update-password">修改密码</el-dropdown-item>
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>

        <!-- tab标签页区域 - 用于标签页切换 -->
        <div class="tabs-switch-page">
          <el-tag
            size="default"
            v-for="(tab, index) in tabList"
            :key="tab.name"
            @close="handleClose(tab, index)"
            @click="changeMenu(tab)"
            :closable="tab.name !== 'adminhome'"
            :effect="this.$store.state.activePath === tab.name ? 'dark' : 'plain'">
          {{tab.label}}
          </el-tag>
        </div>

      </el-header>

      <!-- 内容区 -->
      <el-main>
        <!-- 路由占位符，用于展示内容区的内容 -->
        <div style="padding:15px">
          <keep-alive :include="catch_components" >
            <router-view/>
          </keep-alive>
        </div>
      </el-main>

    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex';

export default {
  name: "AdminHome",
  components: {},
  props: {},
  data() {
    return {
      menuList:[],
      isCollapse:false,
      mottoList:[
        '等风来不如追风去，追逐的过程就是人生的意义',
        '当你想要放弃了，那就想想当初为什么开始',
        '自强之人谁也打不倒，自弃之人谁也带不动',
        '既然无法选择回去的路程，那么就清晰的面对已经造成的挑战',
        '在难过的时候，不要忘记自己还要前进',
        '人生能有几次搏？莫到白发还未博'
      ],
      
    };
  },
  computed: {
    ...mapState({
      activePath:state => StaticRange.activePath,
      tabList:state => state.tabList,
      catch_components:state => state.catch_components
    })
  },
  watch: {

  },
  methods: {
    loadMenu(){
      this.menuList = [
            {
                id: 'number-01', 
                icon:'House',
                path: '/adminhome', 
                label: '系统首页', 
                name: 'adminhome'
            },
            {
                id: 'number-02', 
                icon:'Star',
                path: '/test2', 
                label: '用户管理', 
                name: 'test2'
            },
            {
                id: 'number-03', 
                icon:'location',
                path: '/test3', 
                label: '文章管理', 
                name: 'test3'
            },
            {
                id: 'number-04', 
                icon:'location',
                path: '/test4', 
                label: '个人中心', 
                name: 'test4'
            },
            {
                id: 'number-05', 
                icon:'location',
                path: '/test5', 
                label: '投诉审核', 
                name: 'test5'
            },
            // {
            //     id: 'number-06', 
            //     icon:'location',
            //     path: '/test6', 
            //     label: '测试页面6', 
            //     name: 'test6'
            // },
            // {
            //     id: 'number-07', 
            //     icon:'location',
            //     path: '/test7', 
            //     label: '测试页面7', 
            //     name: 'test7'
            // },
        ]
    },
    handleCommand(command) {
        switch (command) {
          case 'logout':
            this.$router.push('/')
            break;
          case 'update-password':
            this.$router.push('/test4')
            break;
          default:
            break;
        }
    },
    toggleCollapse(){
      this.isCollapse = !this.isCollapse;
    },
    selectMenu(item){
      this.$store.commit('addKeepAliveCache',item.name);
      var name = item.name === 'wellcome'? 'adminhome':item.name;
      var submenu = {
        path:name,
        name:name,
        label:item.label
      }
      this.$store.commit('selectMenu',submenu)
      console.log(this.$store.state.activePath);
    },
    handleClose(tab,index){
      var oldActivePath = this.$store.state.activePath;
      var oldTabList = this.$store.state.tabList;
      // 计算标签个数,-1是因为首页不允许关闭
      let length = oldTabList.length - 1
      for (let i = 0; i < oldTabList.length; i++) {
        let item = oldTabList[i];
        if (item.name === tab.name) {
          oldTabList.splice(i,1);
          break;
        }
      }
      this.$store.commit('removeKeepAliveCache',tab.name);
      if(tab.name !== oldActivePath){
        return
      }
      if(length === 1){
        this.$store.commit('closeTab',{activePath:'adminhome',tabList:oldTabList})
        this.$router.push({name:oldTabList[index-1].name})
        return
      }
      if(index === length){
        oldActivePath = oldTabList[index-1].name;
        this.$store.commit('closeTab',{activePath:oldActivePath,tabList:oldTabList})
        this.$router.push({name:oldTabList[index-1].name})
      }else{
        oldActivePath = oldTabList[index].name;
        this.$store.commit('closeTab',{activePath:oldActivePath,tabList:oldTabList})
        this.$router.push({name:oldTabList[index].name})
      }
    },
    changeMenu(item){
      var oldActivePath = this.$store.state.activePath;
      if(oldActivePath === item.name){
        return
      }
      this.$store.commit('changeMenu',item.name)
      this.$router.push({name:item.name})
      console.log(this.$store.state.activePath);
    }

  },
  created() {
    if(JSON.stringify(this.$store.state.user)  == '{}'){
      this.$router.push('/login')
      return
    }
    this.loadMenu();
  },
  mounted() {},
};
</script>

<style lang="less" scoped>
.el-col-align-middle{
    line-height: 40px;
    text-align: left;
    font-size: 14px;
}
.home-container{
  height: 100%;
}
.el-header{
  color: rgb(0, 0, 0);
  font-size: 20px;
  border-bottom: 1px solid #dddddd;
  height: 103px !important;
  padding: 0;
  background: #fff;
}
.header-row{
  height:60px;
  width:100%;
  display: flex;
  flex-direction:row;
  justify-content: center;
  border-bottom:1px solid #dddddd;
  overflow: hidden;
}
.header-avatar{
  float:right;
  width:40%;
  display: flex;
  align-items: center;
  justify-content:flex-end;
  padding-right:20px;
  .user{
    font-size: 14px;
    font-weight: bold;
    padding: 0 10px;
  }
}

.el-aside{
  background-color: #282C34;
  .header-title{
    padding-left: 10px;
    height: 60px;
    font-weight: 300;
    display: flex;
    font-size: 20px;
    align-items: center;
    cursor: pointer;
    color: #ffffff;
    span{
      margin-left: 10px;
    }
  }
  .header-title-hiddle{
    width: 64px;
    height: 60px;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    color: #ffffff;
    cursor: pointer;
  }
  .el-menu{
    border-right: none;
  }
}
// 菜单选中背景色
.el-menu-item.is-active{
  background-color: #1890FF !important;
  color: yellow !important;
}
// 菜单悬浮背景色
.el-menu-item:hover{
  background-color: #1890FF !important;
}
// 走马灯
.el-carousel__item h3 {
  color: #ee7c12;
  font-size: 14px;
  opacity: 0.75;
  line-height: 25px;
  margin: 0;
}

.el-main{
  background-color: #eaedf1;
  padding: 0;
}

.fa{
  margin-right: 10px;
}

// 点击展开/折叠按钮
.toggle-button{
  width: 80%;
  font-size: 20px;
  line-height: 40px;
  color:#595959;
  text-align: left;
  display: flex;
  align-items: center;
  float:left;
  padding-left: 20px;
  i{
    cursor: pointer;
  }
}
// 面包屑导航
.el-breadcrumb{
  margin-bottom: 0;
}

// tab页
.tabs-switch-page{
  display: flex;
  align-items:center;
  height: 40px;
  background-color:#fff;
  overflow: hidden;
}
.el-tag{
  cursor: pointer;
  margin-left: 10px;
  border-radius: 2px;
  font-size: 12px;
  color: #1890FF;
  border-color: #1890FF;
}
.el-tag--dark{
  color: #fff;
  background-color: #1890FF;
}

.el-dropdown-link {
  cursor: pointer;
}
.el-icon-arrow-down {
  font-size: 12px;
}

.submit-row{
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    align-items: center;
}
</style>
